﻿Public Class DangNhap

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Dim kiemtra As New CheckClass
    Dim cn As New DatabaseConnect

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If txtUN.Text <> "" And txtPW.Text <> "" Then
            If kiemtra.IsExist("NhanVien", " where taikhoan = '" + txtUN.Text + "' and matkhau ='" + txtPW.Text + "'") = True Then
                Main.flag_login = 1
                Dim dt As New DataTable
                dt = cn.GetData("select * from NhanVien where taikhoan ='" + txtUN.Text + "'")
                Dim tenNV As String = Convert.ToString(dt.Rows(0)("hotenNV"))
                Dim idNV As String = Convert.ToString(dt.Rows(0)("MaNV"))
                Main.NVid = idNV
                Main.NVname = tenNV
                Me.Close()
            ElseIf txtUN.Text = "Admin" And txtPW.Text = "hehehe" Then
                Main.flag_login = 1
                Main.NVid = 0
                Main.NVname = "ADMIN"
                Me.Close()
            Else
                MessageBox.Show("Tài khoản hoặc mật khẩu không đúng")
                Return
            End If
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub DangNhap_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtUN.Text <> "" And txtPW.Text <> "" Then
                If kiemtra.IsExist("NhanVien", " where taikhoan = '" + txtUN.Text + "' and matkhau ='" + txtPW.Text + "'") = True Then
                    Main.flag_login = 1
                    Dim dt As New DataTable
                    dt = cn.GetData("select * from NhanVien where taikhoan ='" + txtUN.Text + "'")
                    Dim tenNV As String = Convert.ToString(dt.Rows(0)("hotenNV"))
                    Dim idNV As Integer = Convert.ToInt32(dt.Rows(0)("MaNV"))
                    Main.NVid = idNV
                    Main.NVname = tenNV
                    Me.Close()
                ElseIf txtUN.Text = "Admin" And txtPW.Text = "hehehe" Then
                    Main.flag_login = 1
                    Main.NVid = 0
                    Main.NVname = "ADMIN"
                    Me.Close()
                Else
                    MessageBox.Show("Tài khoản hoặc mật khẩu không đúng")
                    Return
                End If
            End If
        ElseIf e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub
End Class
